from jili.calc import calcor_base
import pyfinance as pf
"""
一类:
N期统计:最大回撤,年华收益率,超额收益率,夏普比率,信息比率,最大回撤,最大回撤时间,最大回撤时间,最大回撤时间
一类:
自由统计:最大回撤,年华收益率,超额收益率,夏普比率,信息比率,最大回撤,最大回撤时间,最大回撤时间,最大回撤时间

场景:
原始数据场景:自动计算需要的中间算子
日收益率场景:_r
def performance(code,start='2011-01-01',end=''):
    tss=get_data(code,start,end)
    benchmark=get_data('hs300',start,end).loc[tss.index]
    dd={}
    #收益率
    #年化收益率
    dd['年化收益率']=tss.anlzd_ret()
    #累积收益率
    dd['累计收益率']=tss.cuml_ret()
    #风险指标
    #年化标准差
    dd['年化标准差']=tss.anlzd_stdev()
    #下行标准差
    dd['下行标准差']=tss.semi_stdev()
        #风险调整收益率
    dd['夏普比率']=tss.sharpe_ratio()
    dd['索提诺比率']=tss.sortino_ratio(freq=250)
    dd['calmar比率']=tss.calmar_ratio()
    #最大回撤
    dd['最大回撤']=tss.max_drawdown()
    #信息比率和特雷诺指数
    dd['信息比率']=tss.info_ratio(benchmark)
    dd['特雷纳指数']=tss.treynor_ratio(benchmark)
        #alpha和beta
    dd['alpha']=tss.alpha(benchmark)
    dd['beta']=tss.beta(benchmark)
    df=pd.DataFrame(dd.values(),index=dd.keys()).round(4)
    return df
"""
class pf_anlzd_ret(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1=pf.TSeries(data1)
        data1.freq=self.freq
        r=data1.anlzd_ret()
        return r
class pf_cuml_ret(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1=pf.TSeries(data1)
        data1.freq=self.freq
        r=data1.cuml_ret()
        return r
class pf_anlzd_stdev(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1=pf.TSeries(data1)
        data1.freq=self.freq
        r=data1.anlzd_stdev()
        return r
class pf_semi_stdev(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1=pf.TSeries(data1)
        data1.freq=self.freq
        r=data1.semi_stdev()
        return r
class pf_max_drawdown(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1=pf.TSeries(data1)
        data1.freq=self.freq
        r=data1.max_drawdown()
        return r
class pf_sharpe_ratio(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1 = pf.TSeries(data1)
        data1.freq = self.freq
        r = data1.sharpe_ratio()
        return r
class pf_sortino_ratio(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1 = pf.TSeries(data1)
        data1.freq = self.freq
        r = data1.sortino_ratio()
        return r
class pf_calmar_ratio(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        if self.is_notror:
            data1 = data0.pct_change()
            data1 = data1.dropna()
        else:
            data1=data0
        data1 = pf.TSeries(data1)
        data1.freq = self.freq
        r = data1.calmar_ratio()
        return r
class pf_info_ratio(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close","close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        data1 = self.hisbars[self.input[0]]
        if self.is_notror:
            data00 = data0.pct_change()
            data00 = data00.dropna()
            data10 = data1.pct_change()
            data10 = data10.dropna()
        else:
            data00=data0
            data10 = data1
        data00 = pf.TSeries(data00)
        data00.freq = self.freq
        data10 = pf.TSeries(data10)
        data10.freq = self.freq
        r = data00.info_ratio(data10)
        return r
class pf_treynor_ratio(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close","close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        data1 = self.hisbars[self.input[0]]
        if self.is_notror:
            data00 = data0.pct_change()
            data00 = data00.dropna()
            data10 = data1.pct_change()
            data10 = data10.dropna()
        else:
            data00=data0
            data10 = data1
        data00 = pf.TSeries(data00)
        data00.freq = self.freq
        data10 = pf.TSeries(data10)
        data10.freq = self.freq
        r = data00.treynor_ratio(data10)
        return r
class pf_alpha(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close","close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        data1 = self.hisbars[self.input[0]]
        if self.is_notror:
            data00 = data0.pct_change()
            data00 = data00.dropna()
            data10 = data1.pct_change()
            data10 = data10.dropna()
        else:
            data00=data0
            data10 = data1
        data00 = pf.TSeries(data00)
        data00.freq = self.freq
        data10 = pf.TSeries(data10)
        data10.freq = self.freq
        r = data00.alpha(data10)
        return r
class pf_beta(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close","close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        data1 = self.hisbars[self.input[0]]
        if self.is_notror:
            data00 = data0.pct_change()
            data00 = data00.dropna()
            data10 = data1.pct_change()
            data10 = data10.dropna()
        else:
            data00=data0
            data10 = data1
        data00 = pf.TSeries(data00)
        data00.freq = self.freq
        data10 = pf.TSeries(data10)
        data10.freq = self.freq
        r = data00.beta(data10)
        return r
class pf_excess_ret(calcor_base):
    def __init__(self,ta_arg={}):
        self.out = ["ror"]
        self.input = ["close","close"]
        self.parameters = {"timeperiod":20,"is_notror":True,"freq":"D"}
        super().__init__(ta_arg)
        if self.parameters["is_notror"]:
            self.is_notror = True
            self.batch=self.parameters["timeperiod"]+1
        else:
            self.batch=self.parameters["timeperiod"]
            self.is_notror = False
        self.freq=self.parameters["freq"]
    def calc(self):
        data0=self.hisbars[self.input[0]]
        data1 = self.hisbars[self.input[0]]
        if self.is_notror:
            data00 = data0.pct_change()
            data00 = data00.dropna()
            data10 = data1.pct_change()
            data10 = data10.dropna()
        else:
            data00=data0
            data10 = data1
        data00 = pf.TSeries(data00)
        data00.freq = self.freq
        data10 = pf.TSeries(data10)
        data10.freq = self.freq
        c3 = data00.excess_ret(data10)
        r=c3.anlzd_ret()
        return r